package com.ygqh.baby.controller.admin;

import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.ao.YgQuartzJob;
import com.ygqh.baby.model.AddScheduleJobParam;
import com.ygqh.baby.model.SchJob;
import com.ygqh.baby.po.AdminUser;
import com.ygqh.baby.po.ScheduleJob;
import com.ygqh.baby.service.ScheduleJobService;
import com.ygqh.baby.shiro.utils.SessionUtil;
import com.ygqh.baby.utils.DateConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@RequestMapping("/admin/job")
@Controller
public class ScheduleJobController extends BaseController<Object, java.lang.Long> {

    @Autowired
    private ScheduleJobService scheduleJobService;

    @RequestMapping("searchJob")
    @ResponseBody
    public Map<String, Object> searchJob(String q, QueryInfo queryInfo, Date startDate, Date endDate, String groupName ) {
        ResultSet<ScheduleJob> resultSet = scheduleJobService.search(queryInfo, q, groupName, startDate, endDate);
        return resultSet.toDatatables(null);
    }

    @RequestMapping("getJobFromQuartz")
    @ResponseBody
    public Map<String, Object> getJobFromQuartz(String groupName, String jobName) {
        List<ScheduleJob> jobList = scheduleJobService.getJobFromQuartz(groupName, jobName);
        return new ResultSet<>(jobList.size(), jobList).toDatatables(null);
    }

    @RequestMapping(value = "saveJob", method = RequestMethod.POST)
    @ResponseBody
    public Message saveJob(AddScheduleJobParam param) {
        AdminUser user = SessionUtil.getCurrentUser();
        Date executeTime = DateConvertUtils.parse(param.getExecuteTime(), DateConvertUtils.DATE_TIME_FORMAT);
        return scheduleJobService.addJob(param.getJob(), param.getName(), executeTime, param.getParam(), user.getTrueName());
    }

    @RequestMapping(value = "saveJob1", method = RequestMethod.POST)
    @ResponseBody
    public Message saveJob1(@RequestBody AddScheduleJobParam param) {
        AdminUser user = SessionUtil.getCurrentUser();
        Date executeTime = DateConvertUtils.parse(param.getExecuteTime(), DateConvertUtils.DATE_TIME_FORMAT);
        return scheduleJobService.addJob(param.getJob(), param.getName(), executeTime, param.getParam(), user.getTrueName());
    }

    @RequestMapping(value = "getAllQuartzJob", method = RequestMethod.GET)
    @ResponseBody
    public Message getAllQuartzJob() {
        YgQuartzJob[] values = YgQuartzJob.values();
        List<String> groupList = new ArrayList<>();
        for (int i = 0; i < values.length; i++) {
            if (groupList.contains(values[i].getJobGroup())) {
                continue;
            }
            groupList.add(values[i].getJobGroup());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("groupList", groupList);
        map.put("jobType", values);
        return Message.success(map);
    }

    @RequestMapping(value = "updateJob", method = RequestMethod.POST)
    @ResponseBody
    public Message updateJob(String jobName, String jobGroup, String runDate) {
        AdminUser user = SessionUtil.getCurrentUser();
        ScheduleJob job = scheduleJobService.getJob(jobName, jobGroup);
        Date cronDate = DateConvertUtils.parse(runDate, DateConvertUtils.DATE_TIME_FORMAT);
        String cron = DateConvertUtils.format(cronDate, DateConvertUtils.DATE_FORMAT_CRON);
        job.setCron(cron);
        job.setJobStatus(SchJob.STATUS_RUNNING);
        job.setJobNum(1);
        Message message = scheduleJobService.updateJobToSchedule(job);
        if (Message.isSuccess(message)) {

            scheduleJobService.updateCronByJobKey(job.getJobName(), job.getJobGroup(), cron, cronDate, user.getTrueName());
        }
        return message;
    }

    @RequestMapping(value = "/getRunningJob")
    @ResponseBody
    public Message getRunningJob() {

        return Message.success("");
    }

    @RequestMapping(value = "/getAllJob")
    @ResponseBody
    public Message getAllJob() {
        List<SchJob> list = scheduleJobService.getAllJobFromSchedule();
        return Message.success(list);
    }

    @RequestMapping(value = "/deleteJob")
    @ResponseBody
    public Message deleteJob(String jobName, String jobGroup) {
        AdminUser user = SessionUtil.getCurrentUser();
        return scheduleJobService.deleteJob(jobName, jobGroup, user.getTrueName());
    }

}
